////////import org.openqa.selenium.*;
////////import org.openqa.selenium.chrome.ChromeDriver;
////////import org.openqa.selenium.chrome.ChromeOptions;
////////import org.openqa.selenium.interactions.Actions;
////////import org.openqa.selenium.support.ui.ExpectedConditions;
////////import org.openqa.selenium.support.ui.WebDriverWait;
////////
////////import java.io.File;
////////import java.time.Duration;
////////import java.util.List;
////////import java.util.Random;
////////import java.util.Set;
////////
////////public class BossGo {
////////    static  String msg = "您好！作为转行Java开发者，我已完成SpringBoot+Redis+Mybatis等技能点的系统学习。在游戏陪玩平台系统中独立开发核心模块，增删改查功能。附件是我的简历，希望可与您进一步沟通！";
////////
////////    static Integer  num =0;
////////
////////    public static void main(String[] args) throws Exception {
////////        ChromeOptions options = new ChromeOptions();
////////        options.addArguments("--disable-blink-features=AutomationControlled");
////////        options.addArguments("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36");
////////
////////        System.setProperty("webdriver.chrome.driver", "D:\\RuanJian\\Jdk11\\bin\\chromedriver.exe");
////////        WebDriver webDriver = new ChromeDriver(options);
////////
////////      // 隐藏 webdriver 标识
////////        ((JavascriptExecutor) webDriver).executeScript("delete navigator.__proto__.webdriver;");
////////
////////        webDriver.get("https://www.zhipin.com");
////////
////////        // 如果已经存在 Cookie 文件，则直接加载登录状态
////////        File cookieFile = new File("boss_cookie.dat");
////////        if (cookieFile.exists()) {
////////            Set<Cookie> cookies = CookieUtils.loadCookies(cookieFile.getAbsolutePath());
////////            for (Cookie cookie : cookies) {
////////                webDriver.manage().addCookie(cookie);
////////            }
////////            webDriver.navigate().refresh();
////////            Thread.sleep(5000);
////////
////////            // 提示用户选择页面
////////            System.out.println("已从 Cookie 登录，请在 15 秒内选择好要沟通的岗位页面...");
////////            int time = 0;
////////            int timeout = 1000 * 15;
////////            while (time < timeout) {
////////                Thread.sleep(1000);
////////                time += 1000;
////////                System.out.println("还剩 " + (timeout - time) / 1000 + " 秒，请选择岗位页面！");
////////            }
////////
////////        } else {
////////            // 第一次运行，需要手动登录
////////            System.out.println("请在半分钟内手动登录 Boss 直聘账号...");
////////            int time = 0;
////////            int timeout = 1000 * 60 / 2;
////////            while (time < timeout) {
////////                Thread.sleep(1000);
////////                time += 1000;
////////                System.out.println("还剩 " + (timeout - time) / 1000 + " 秒，请登录！");
////////            }
////////
////////            // 登录完成后保存 Cookie
////////            CookieUtils.saveCookies(webDriver, "boss_cookie.dat");
////////        }
////////
////////        System.out.println("自动沟通开始了-----");
////////
////////        // 开始遍历岗位
////////        for (int page = 0; page < 3; page++) {
////////            List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
////////            int jobNum = jobList.size();
////////
////////            for (int i = 0; i < jobNum; i++) {
////////                List<WebElement> currentJobList = webDriver.findElements(By.className("job-card-wrap"));
////////                if (i >= currentJobList.size()) {
////////                    System.out.println("当前索引越界，跳过：" + i);
////////                    continue;
////////                }
////////
////////                try {
////////                    hi(webDriver, i);
////////                    System.out.println("已经执行一个打招呼了");
////////                } catch (Exception e) {
////////                    System.out.println("报错了：" + e.getMessage());
////////
////////                    Thread.sleep(5000);
////////                }
////////            }
////////
////////            try {
////////                scrollToBottomWithInfiniteLoad(webDriver);
////////            } catch (Exception e) {
////////                System.out.println("执行到这里了，好像出错了");
////////            }
////////
////////            Thread.sleep(5000);
////////
////////        }
////////
////////    }
////////
//////////    private static void hi(WebDriver webDriver, int index) throws InterruptedException{
//////////
//////////
//////////        //回退页面元素会刷新，需要重新获取一遍
//////////        List<WebElement>  jobList = webDriver.findElements(By.className("job-card-wrap"));
//////////        for (int i = 0; i < jobList.size(); i++) {
//////////            if(i == index){
//////////                try{
//////////                    //使用Action类进行悬浮
//////////                    Actions actions = new Actions(webDriver);
//////////                    actions.moveToElement(jobList.get(i)).perform();
//////////                    WebElement btn = jobList.get(i).findElement(By.className("op-btn"));
//////////                    if(btn.getText().equals("继续沟通")){
//////////                        break;
//////////
//////////                    }
//////////                    //点击沟通按钮
//////////                    btn.click();
//////////                        num++;
//////////
//////////                }catch (Exception e){
//////////                    break;
//////////                }
//////////
//////////                Thread.sleep(1000);
//////////                //打招呼的消息
//////////                webDriver.findElement(By.className("chat-input")).sendKeys(msg);
//////////                //发送
//////////                webDriver.findElement(By.className("chat-input")).sendKeys(Keys.ENTER);
//////////                System.out.println("沟通此时： " + num);
//////////                Thread.sleep(1000);
//////////                //回退
//////////                webDriver.navigate().back();
//////////                Thread.sleep(2000);
//////////                break;
//////////
//////////            }
//////////        }
//////////
//////////
//////////    }
//////////    private static void hi(WebDriver webDriver, int index) throws InterruptedException {
//////////        List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
//////////        for (int i = 0; i < jobList.size(); i++) {
//////////            if (i == index) {
//////////                try {
//////////                    Actions actions = new Actions(webDriver);
//////////                    actions.moveToElement(jobList.get(i)).perform();
//////////                    WebElement btn = jobList.get(i).findElement(By.cssSelector(".op-btn.op-btn-chat"));
//////////                    if (btn.getText().equals("继续沟通")) {
//////////                        System.out.println("该职位已沟通，跳过");
//////////                        break;
//////////                    }
//////////                    btn.click();
//////////                    num++;
//////////
//////////                    // 显式等待输入框可见
//////////                    WebDriverWait wait = new WebDriverWait(webDriver, 10);
//////////                    wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("chat-input")));
//////////
//////////                    // 打招呼
//////////                    WebElement chatInput = jobList.get(i).findElement(By.cssSelector(".op-btn.op-btn-chat"));
//////////
//////////                    chatInput.sendKeys(msg);
//////////                    chatInput.sendKeys(Keys.ENTER);
//////////                    System.out.println("沟通此时： " + num);
//////////
//////////                } catch (Exception e) {
//////////                    System.err.println("发生异常：" + e.getMessage());
//////////                    break;
//////////                }
//////////
//////////                Thread.sleep(1000);
//////////                webDriver.navigate().back();
//////////                Thread.sleep(2000);
//////////                break;
//////////            }
//////////        }
//////////    }
////////
////////
////////
////////
////////
//////////    private static void hi(WebDriver webDriver, int index) throws InterruptedException {
//////////        List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
//////////        for (int i = 0; i < jobList.size(); i++) {
//////////            if (i == index) {
//////////                try {
//////////                    WebElement jobElement = jobList.get(i);
//////////
//////////                    // 获取职位名称
//////////                    String jobName = jobElement.findElement(By.cssSelector(".job-name")).getText();
//////////
//////////                    // 获取公司名称
//////////                    String companyName = jobElement.findElement(By.cssSelector(".boss-name")).getText();
//////////
//////////                    Actions actions = new Actions(webDriver);
//////////                    actions.moveToElement(jobElement).perform();
//////////
//////////                    WebDriverWait wait = new WebDriverWait(webDriver, 10);
//////////                    WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(
//////////                            By.cssSelector(".op-btn.op-btn-chat")
//////////                    ));
//////////
//////////                    // 打印当前处理的职位信息
//////////                    System.out.println("正在处理职位：" + jobName + " - 公司：" + companyName);
//////////
//////////                    if (btn.getText().equals("继续沟通")) {
//////////                        System.out.println("该职位已沟通，跳过：" + jobName + " - 公司：" + companyName);
//////////                        break;
//////////                    }
//////////
//////////                    btn.click();
//////////                    num++;
//////////
//////////                    // 等待聊天框出现并发送消息
//////////                    WebElement chatInput = wait.until(ExpectedConditions.visibilityOfElementLocated(
//////////                            By.className("chat-input")
//////////                    ));
//////////                    chatInput.sendKeys(msg);
//////////                    chatInput.sendKeys(Keys.ENTER);
//////////
//////////                    System.out.println("沟通此时： " + num + "，职位：" + jobName + " - 公司：" + companyName);
//////////
//////////                } catch (Exception e) {
//////////                    System.err.println("发生异常：" + e.getMessage());
//////////                    break;
//////////                }
//////////
//////////                Thread.sleep(1000);
//////////                webDriver.navigate().back();
//////////                Thread.sleep(2000);
//////////                break;
//////////            }
//////////        }
//////////    }
////////
//////////    private static void hi(WebDriver webDriver, int index) throws InterruptedException {
//////////        List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
//////////        for (int i = 0; i < jobList.size(); i++) {
//////////            if (i == index) {
//////////                try {
//////////                    WebElement jobElement = jobList.get(i);
//////////
//////////                    String jobName = jobElement.findElement(By.cssSelector(".job-name")).getText();
//////////                    String companyName = jobElement.findElement(By.cssSelector(".boss-name")).getText();
//////////
//////////                    System.out.println("正在处理职位：" + jobName + " - 公司：" + companyName);
//////////
//////////                    Actions actions = new Actions(webDriver);
//////////                    actions.moveToElement(jobElement).perform();
//////////
//////////                    // 模拟轻微鼠标移动
//////////                    for (int j = 0; j < 3; j++) {
//////////                        actions.moveByOffset(5, 5).perform();
//////////                        actions.moveByOffset(-5, -5).perform();
//////////                        waitRandomTime(100, 300);
//////////                    }
//////////
//////////                    WebDriverWait wait = new WebDriverWait(webDriver, 10);
//////////                    WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(
//////////                            By.cssSelector(".op-btn.op-btn-chat")
//////////                    ));
//////////
//////////                    String buttonText = (String) ((JavascriptExecutor) webDriver).executeScript(
//////////                            "return arguments[0].textContent.trim();", btn);
//////////
//////////                    if ("继续沟通".equals(buttonText)) {
//////////                        System.out.println("该职位已沟通，跳过：" + jobName + " - 公司：" + companyName);
//////////                        break;
//////////                    }
//////////
//////////                    btn.click();
//////////                    num++;
//////////
//////////                    // 输入打招呼信息
//////////                    WebElement chatInput = wait.until(ExpectedConditions.visibilityOfElementLocated(
//////////                            By.className("chat-input")
//////////                    ));
//////////
//////////                    char[] chars = msg.toCharArray();
//////////                    for (char c : chars) {
//////////                        chatInput.sendKeys(String.valueOf(c));
//////////                        waitRandomTime(100, 300);
//////////                    }
//////////                    chatInput.sendKeys(Keys.ENTER);
//////////
//////////                    System.out.println("沟通此时： " + num + "，职位：" + jobName + " - 公司：" + companyName);
//////////
//////////                } catch (Exception e) {
//////////                    System.err.println("发生异常：" + e.getMessage());
//////////                    break;
//////////                }
//////////
//////////                waitRandomTime(1000, 2000);
//////////                webDriver.navigate().back();
//////////                waitRandomTime(2000, 4000);
//////////                break;
//////////            }
//////////        }
//////////    }
////////
////////    private static void hi(WebDriver webDriver, int index) throws InterruptedException {
////////        WebDriverWait wait = new WebDriverWait(webDriver, 15);
////////        Actions actions = new Actions(webDriver);
////////
////////        // 获取职位列表
////////        List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
////////        if (index >= jobList.size()) {
////////            System.out.println("索引越界，跳过：" + index);
////////            return;
////////        }
////////
////////        WebElement jobElement = jobList.get(index);
////////        String jobName = jobElement.findElement(By.cssSelector(".job-name")).getText();
////////        String companyName = jobElement.findElement(By.cssSelector(".boss-name")).getText();
////////
////////        System.out.println("正在处理职位：" + jobName + " - 公司：" + companyName);
////////
////////        try {
////////            // ====== 模拟自然鼠标移动和点击 ======
////////            actions.moveToElement(jobElement)
////////                    .pause(Duration.ofMillis(500))
////////                    .moveByOffset(-5, -5)
////////                    .moveByOffset(5, 5)
////////                    .click()
////////                    .perform();
////////
////////            waitRandomTime(1000, 2000); // 自然停顿
////////
////////            // ====== 查找沟通按钮并点击 ======
////////            WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(
////////                    By.cssSelector(".op-btn.op-btn-chat")));
////////
////////            String buttonText = (String) ((JavascriptExecutor) webDriver).executeScript(
////////                    "return arguments[0].textContent.trim();", btn);
////////
////////            if (!"立即沟通".equals(buttonText)) {
////////                System.out.println("该职位已沟通，跳过：" + jobName + " - 公司：" + companyName);
////////                return;
////////            }
////////
////////            ((JavascriptExecutor) webDriver).executeScript("arguments[0].click();", btn);
////////            waitRandomTime(3000, 6000); // 等待聊天框加载
////////
////////            // ====== 检查是否有弹窗（如工作经历不匹配）======
////////            try {
////////                WebElement warningModal = webDriver.findElement(By.cssSelector(".chat-block-container"));
////////                if (warningModal.isDisplayed()) {
////////                    System.out.println("检测到‘工作经历不匹配’弹窗，正在尝试关闭...");
////////                    webDriver.findElement(By.cssSelector(".sure-btn")).click();
////////                    waitRandomTime(2000, 4000);
////////                    System.out.println("弹窗已关闭");
////////                }
////////            } catch (Exception ignored) {}
////////
////////            // ====== 发送打招呼信息 ======
////////            WebElement chatInput = wait.until(ExpectedConditions.visibilityOfElementLocated(
////////                    By.className("chat-input")));
////////
////////            for (char c : msg.toCharArray()) {
////////                chatInput.sendKeys(String.valueOf(c));
////////                waitRandomTime(100, 300);
////////            }
////////            chatInput.sendKeys(Keys.ENTER);
////////            num++;
////////            System.out.println("沟通第 " + num + " 个职位：" + jobName + " - 公司：" + companyName);
////////
////////        } catch (TimeoutException e) {
////////            System.err.println("等待元素超时：" + e.getMessage());
////////        } catch (NoSuchElementException e) {
////////            System.err.println("找不到指定元素：" + e.getMessage());
////////        } catch (Exception e) {
////////            System.err.println("发生异常：" + e.getMessage());
////////        }
////////
////////        // ====== 返回上一页 ======
////////        ((JavascriptExecutor) webDriver).executeScript("window.history.back();");
////////        try {
////////            wait.until(ExpectedConditions.presenceOfElementLocated(By.className("job-card-wrap")));
////////        } catch (Exception e) {
////////            System.out.println("页面恢复失败：" + e.getMessage());
////////        }
////////
////////        waitRandomTime(2000, 4000); // 下一轮前休息一下
////////        System.out.println("页面已恢复，继续下一个");
////////    }
////////
////////
////////
////////
////////
////////
////////
////////    private static void waitRandomTime(int min, int max) throws InterruptedException {
////////        int randomTime = new Random().nextInt(max - min) + min;
////////        Thread.sleep(randomTime);
////////    }
////////
////////    public static void scrollToBottomWithInfiniteLoad(WebDriver driver) throws InterruptedException {
////////        String script = "var totalHeight = document.body.scrollHeight;"
////////                + "var scrollTop = 0;"
////////                + "var attempts = 0;"
////////                + "var maxAttempts = 5;"  // 最多尝试 5 次无新增高度即停止
////////                + "var interval = setInterval(function() {"
////////                + "    scrollTop = document.body.scrollTop;"
////////                + "    window.scrollTo(0, document.body.scrollHeight);"
////////                + "    attempts++;"
////////                + "    if (document.body.scrollHeight == totalHeight && attempts >= maxAttempts) {"
////////                + "        clearInterval(interval);"
////////                + "    }"
////////                + "    totalHeight = document.body.scrollHeight;"
////////                + "}, 1000);";
////////
////////        ((JavascriptExecutor) driver).executeScript(script);
////////        Thread.sleep(5000); // 给足时间让 JS 执行完毕
////////    }
////////
////////
////////}
//////import org.openqa.selenium.*;
//////import org.openqa.selenium.chrome.ChromeDriver;
//////import org.openqa.selenium.chrome.ChromeOptions;
//////import org.openqa.selenium.interactions.Actions;
//////import org.openqa.selenium.support.ui.ExpectedConditions;
//////import org.openqa.selenium.support.ui.WebDriverWait;
//////
//////import java.io.File;
//////import java.time.Duration;
//////import java.util.List;
//////import java.util.Random;
//////import java.util.Set;
//////
//////public class BossGo {
//////    static String msg = "您好！作为转行Java开发者，我已完成SpringBoot+Redis+Mybatis等技能点的系统学习。在游戏陪玩平台系统中独立开发核心模块，增删改查功能。附件是我的简历，希望可与您进一步沟通！";
//////    static Integer num = 0;
//////
//////    public static void main(String[] args) throws Exception {
//////        ChromeOptions options = new ChromeOptions();
//////        options.addArguments("--disable-blink-features=AutomationControlled");
//////        options.addArguments("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36");
//////
//////        System.setProperty("webdriver.chrome.driver", "D:\\RuanJian\\Jdk11\\bin\\chromedriver.exe");
//////        WebDriver webDriver = new ChromeDriver(options);
//////
//////        ((JavascriptExecutor) webDriver).executeScript("delete navigator.__proto__.webdriver;");
//////
//////        webDriver.get("https://www.zhipin.com");
//////
//////        File cookieFile = new File("boss_cookie.dat");
//////        if (cookieFile.exists()) {
//////            Set<Cookie> cookies = CookieUtils.loadCookies(cookieFile.getAbsolutePath());
//////            for (Cookie cookie : cookies) {
//////                webDriver.manage().addCookie(cookie);
//////            }
//////            webDriver.navigate().refresh();
//////            Thread.sleep(5000);
//////
//////            System.out.println("已从 Cookie 登录，请在 15 秒内选择好要沟通的岗位页面...");
//////            int time = 0;
//////            int timeout = 1000 * 15;
//////            while (time < timeout) {
//////                Thread.sleep(1000);
//////                time += 1000;
//////                System.out.println("还剩 " + (timeout - time) / 1000 + " 秒，请选择岗位页面！");
//////            }
//////
//////        } else {
//////            System.out.println("请在半分钟内手动登录 Boss 直聘账号...");
//////            int time = 0;
//////            int timeout = 1000 * 60 / 2;
//////            while (time < timeout) {
//////                Thread.sleep(1000);
//////                time += 1000;
//////                System.out.println("还剩 " + (timeout - time) / 1000 + " 秒，请登录！");
//////            }
//////            CookieUtils.saveCookies(webDriver, "boss_cookie.dat");
//////        }
//////
//////        System.out.println("自动沟通开始了-----");
//////
//////        for (int page = 0; page < 3; page++) {
//////            List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
//////            for (int i = 0; i < jobList.size(); i++) {
//////                try {
//////                    hi(webDriver, i);
//////                    System.out.println("已经执行一个打招呼了");
//////                } catch (Exception e) {
//////                    System.out.println("报错了：" + e.getMessage());
//////                    Thread.sleep(5000);
//////                }
//////            }
//////
//////            try {
//////                scrollToBottomWithInfiniteLoad(webDriver);
//////            } catch (Exception e) {
//////                System.out.println("执行到这里了，好像出错了");
//////            }
//////
//////            Thread.sleep(5000);
//////        }
//////    }
//////
//////    private static void hi(WebDriver webDriver, int index) throws InterruptedException {
//////        WebDriverWait wait = new WebDriverWait(webDriver, 15);
//////        Actions actions = new Actions(webDriver);
//////
//////        List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
//////        if (index >= jobList.size()) {
//////            System.out.println("索引越界，跳过：" + index);
//////            return;
//////        }
//////
//////        WebElement jobElement = jobList.get(index);
//////        String jobName = "";
//////        String companyName = "";
//////
//////        try {
//////            jobName = jobElement.findElement(By.cssSelector(".job-name")).getText();
//////            companyName = jobElement.findElement(By.cssSelector(".boss-name")).getText();
//////        } catch (Exception e) {
//////            System.out.println("获取职位/公司名称失败，跳过");
//////            return;
//////        }
//////
//////        System.out.println("正在处理职位：" + jobName + " - 公司：" + companyName);
//////
//////        actions.moveToElement(jobElement)
//////                .pause(Duration.ofMillis(500))
//////                .moveByOffset(-5, -5)
//////                .moveByOffset(5, 5)
//////                .click()
//////                .perform();
//////
//////        waitRandomTime(1000, 2000);
//////
//////        WebElement btn;
//////        try {
//////            btn = wait.until(ExpectedConditions.elementToBeClickable(
//////                    By.cssSelector(".op-btn.op-btn-chat")));
//////        } catch (Exception e) {
//////            System.err.println("找不到沟通按钮，跳过该职位");
//////            return;
//////        }
//////
//////        String buttonText = (String) ((JavascriptExecutor) webDriver).executeScript(
//////                "return arguments[0].textContent.trim();", btn);
//////
//////        if (!"立即沟通".equals(buttonText)) {
//////            System.out.println("该职位已沟通，跳过：" + jobName);
//////            return;
//////        }
//////
//////        ((JavascriptExecutor) webDriver).executeScript("arguments[0].click();", btn);
//////        waitRandomTime(3000, 6000);
//////
//////        try {
//////            WebElement warningModal = webDriver.findElement(By.cssSelector(".chat-block-container"));
//////            if (warningModal.isDisplayed()) {
//////                webDriver.findElement(By.cssSelector(".sure-btn")).click();
//////                waitRandomTime(2000, 4000);
//////            }
//////        } catch (Exception ignored) {}
//////
//////        WebElement chatInput;
//////        try {
//////            chatInput = wait.until(ExpectedConditions.visibilityOfElementLocated(
//////                    By.className("chat-input")));
//////        } catch (Exception e) {
//////            System.err.println("找不到聊天输入框，跳过该职位");
//////            ((JavascriptExecutor) webDriver).executeScript("window.history.back();");
//////            return;
//////        }
//////
//////        // 分段发送信息
//////        String[] parts = msg.split(" ");
//////        for (int i = 0; i < parts.length; i++) {
//////            chatInput.sendKeys(parts[i] + " ");
//////            if ((i + 1) % 5 == 0 || i == parts.length - 1) {
//////                waitRandomTime(200, 600); // 每5个词停顿
//////            }
//////        }
//////        chatInput.sendKeys(Keys.ENTER);
//////        num++;
//////        System.out.println("沟通第 " + num + " 个职位：" + jobName);
//////
//////        ((JavascriptExecutor) webDriver).executeScript("window.history.back();");
//////        try {
//////            wait.until(ExpectedConditions.presenceOfElementLocated(By.className("job-card-wrap")));
//////        } catch (Exception e) {
//////            System.out.println("页面恢复失败：" + e.getMessage());
//////        }
//////
//////        waitRandomTime(2000, 4000);
//////        System.out.println("页面已恢复，继续下一个");
//////    }
//////
//////    private static void waitRandomTime(int min, int max) throws InterruptedException {
//////        int randomTime = new Random().nextInt(max - min) + min;
//////        Thread.sleep(randomTime);
//////    }
//////
//////    public static void scrollToBottomWithInfiniteLoad(WebDriver driver) throws InterruptedException {
//////        String script = "var totalHeight = document.body.scrollHeight;"
//////                + "var scrollTop = 0;"
//////                + "var attempts = 0;"
//////                + "var maxAttempts = 3;"
//////                + "var interval = setInterval(function() {"
//////                + "    window.scrollTo(0, document.body.scrollHeight);"
//////                + "    attempts++;"
//////                + "    if (++attempts > maxAttempts) clearInterval(interval);"
//////                + "}, 2000);";
//////
//////        ((JavascriptExecutor) driver).executeScript(script);
//////        Thread.sleep(8000);
//////    }
////}
//
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.time.Duration;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class BossGo {
    static String msg = "您好！作为转行Java开发者，我已完成SpringBoot+Redis+Mybatis等技能点的系统学习。在游戏陪玩平台系统中独立开发核心模块，增删改查功能。附件是我的简历，希望可与您进一步沟通！";
    static Integer num = 0;

    // 判断是否登录成功
    private static boolean isUserLoggedIn(WebDriver driver) {
        try {
            WebDriverWait wait = new WebDriverWait(driver, 10);
            WebElement userElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".user-name")));
            System.out.println("✅ 登录成功，当前用户：" + userElement.getText());
            return true;
        } catch (Exception e) {
            System.out.println("❌ 页面未检测到登录状态，请检查 Cookie 是否有效");
            return false;
        }
    }

    // 手动登录流程
    private static void manualLogin(WebDriver webDriver, File cookieFile) throws Exception {
        System.out.println("请在半分钟内手动登录 Boss 直聘账号...");
        int time = 0;
        int timeout = 1000 * 60 / 2;
        while (time < timeout) {
            Thread.sleep(1000);
            time += 1000;
            System.out.println("还剩 " + (timeout - time) / 1000 + " 秒，请登录！");
        }

        if (isUserLoggedIn(webDriver)) {
            System.out.println("✅ 手动登录成功，正在保存 Cookie...");
            CookieUtils.saveCookies(webDriver, cookieFile.getAbsolutePath());
        } else {
            System.out.println("❌ 手动登录失败，请确认是否完成登录操作");
        }
    }

    public static void main(String[] args) throws Exception {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-blink-features=AutomationControlled");
        options.addArguments("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36");
        options.addArguments("--window-size=1920,1080");
        options.addArguments("--lang=zh-CN");

        System.setProperty("webdriver.chrome.driver", "D:\\RuanJian\\Jdk11\\bin\\chromedriver.exe");
        WebDriver webDriver = new ChromeDriver(options);

        ((JavascriptExecutor) webDriver).executeScript("delete navigator.__proto__.webdriver;");

        File cookieFile = new File("boss_cookie.dat");

        if (cookieFile.exists()) {
            try {
                Set<Cookie> cookies = CookieUtils.loadCookies(cookieFile.getAbsolutePath());

                webDriver.get("https://www.zhipin.com");
                Thread.sleep(2000);

                for (Cookie cookie : cookies) {
                    webDriver.manage().addCookie(cookie);
                }

                webDriver.navigate().refresh();
                Thread.sleep(5000);

                if (isUserLoggedIn(webDriver)) {
                    System.out.println("✅ 已从 Cookie 登录，请选择岗位页面...");

                    int time = 0;
                    int timeout = 1000 * 15;
                    while (time < timeout) {
                        Thread.sleep(1000);
                        time += 1000;
                        System.out.println("还剩 " + (timeout - time) / 1000 + " 秒，请选择岗位页面！");
                    }

                } else {
                    System.out.println("⚠️ Cookie 登录失败，进入手动登录流程");
                    manualLogin(webDriver, cookieFile);
                }

            } catch (Exception e) {
                System.err.println("⚠️ 加载 Cookie 失败: " + e.getMessage());
                manualLogin(webDriver, cookieFile);
            }
        } else {
            manualLogin(webDriver, cookieFile);
        }

        System.out.println("自动沟通开始了-----");

        for (int page = 0; page < 3; page++) {
            List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
            for (int i = 0; i < jobList.size(); i++) {
                try {
                    hi(webDriver, i);
                    System.out.println("已经执行一个打招呼了");
                } catch (Exception e) {
                    System.out.println("报错了：" + e.getMessage());
                    Thread.sleep(5000);
                }
            }

            try {
                scrollToBottomWithInfiniteLoad(webDriver);
            } catch (Exception e) {
                System.out.println("执行到这里了，好像出错了");
            }

            Thread.sleep(5000);
        }
    }

    private static void hi(WebDriver webDriver, int index) throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(webDriver, 15);
        Actions actions = new Actions(webDriver);

        List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrap"));
        if (index >= jobList.size()) {
            System.out.println("索引越界，跳过：" + index);
            return;
        }

        WebElement jobElement = jobList.get(index);
        String jobName = "";
        String companyName = "";

        try {
            jobName = jobElement.findElement(By.cssSelector(".job-name")).getText();
            companyName = jobElement.findElement(By.cssSelector(".boss-name")).getText();
        } catch (Exception e) {
            System.out.println("获取职位/公司名称失败，跳过");
            return;
        }

        System.out.println("正在处理职位：" + jobName + " - 公司：" + companyName);

        actions.moveToElement(jobElement)
                .pause(Duration.ofMillis(500))
                .moveByOffset(-5, -5)
                .moveByOffset(5, 5)
                .click()
                .perform();

        waitRandomTime(1000, 2000);

        WebElement btn;
        try {
            btn = wait.until(ExpectedConditions.elementToBeClickable(
                    By.cssSelector(".op-btn.op-btn-chat")));
        } catch (Exception e) {
            System.err.println("找不到沟通按钮，跳过该职位");
            return;
        }

        String buttonText = (String) ((JavascriptExecutor) webDriver).executeScript(
                "return arguments[0].textContent.trim();", btn);

        if (!"立即沟通".equals(buttonText)) {
            System.out.println("该职位已沟通，跳过：" + jobName);
            return;
        }

        ((JavascriptExecutor) webDriver).executeScript("arguments[0].click();", btn);
        waitRandomTime(3000, 6000);

        try {
            WebElement warningModal = webDriver.findElement(By.cssSelector(".chat-block-container"));
            if (warningModal.isDisplayed()) {
                webDriver.findElement(By.cssSelector(".sure-btn")).click();
                waitRandomTime(2000, 4000);
            }
        } catch (Exception ignored) {}

        WebElement chatInput;
        try {
            chatInput = wait.until(ExpectedConditions.visibilityOfElementLocated(
                    By.className("chat-input")));
        } catch (Exception e) {
            System.err.println("找不到聊天输入框，跳过该职位");
            ((JavascriptExecutor) webDriver).executeScript("window.history.back();");
            return;
        }

        String[] parts = msg.split(" ");
        for (int i = 0; i < parts.length; i++) {
            chatInput.sendKeys(parts[i] + " ");
            if ((i + 1) % 5 == 0 || i == parts.length - 1) {
                waitRandomTime(200, 600);
            }
        }
        chatInput.sendKeys(Keys.ENTER);
        num++;
        System.out.println("沟通第 " + num + " 个职位：" + jobName);

        ((JavascriptExecutor) webDriver).executeScript("window.history.back();");
        try {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.className("job-card-wrap")));
        } catch (Exception e) {
            System.out.println("页面恢复失败：" + e.getMessage());
        }

        waitRandomTime(2000, 4000);
        System.out.println("页面已恢复，继续下一个");
    }

    private static void waitRandomTime(int min, int max) throws InterruptedException {
        int randomTime = new Random().nextInt(max - min) + min;
        Thread.sleep(randomTime);
    }

    public static void scrollToBottomWithInfiniteLoad(WebDriver driver) throws InterruptedException {
        String script = "var totalHeight = document.body.scrollHeight;"
                + "var scrollTop = 0;"
                + "var attempts = 0;"
                + "var maxAttempts = 3;"
                + "var interval = setInterval(function() {"
                + "    window.scrollTo(0, document.body.scrollHeight);"
                + "    attempts++;"
                + "    if (++attempts > maxAttempts) clearInterval(interval);"
                + "}, 2000);";

        ((JavascriptExecutor) driver).executeScript(script);
        Thread.sleep(8000);
    }
}
